<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="id" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="appid" prop="appid">
        <el-input
          v-model="queryParams.appid"
          placeholder="请输入appid"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="secret" prop="secret">
        <el-input
          v-model="queryParams.secret"
          placeholder="请输入密钥secret"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
       <el-form-item label="牌照类型" prop="status" class="single-line-label">
        <span class="label-spacing"></span>
        <el-select v-model="queryParams.licenseType" clearable placeholder="选择牌照类型" filterable>
          <el-option v-for="(item, i) in extData.statusTypeList || []" :key="i" :label="item.key"
                     :value="item.value"></el-option>
        </el-select>
      </el-form-item>


      <!-- <el-form-item label="创建时间" prop="createTime">
        <el-date-picker clearable
          v-model="queryParams.createTime"
          type="date"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="修改时间" prop="updateTime">
        <el-date-picker clearable
          v-model="queryParams.updateTime"
          type="date"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="请选择修改时间">
        </el-date-picker>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="small"
          @click="handleAdd"
          v-hasPermi="['applet:applet:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-plus"
          size="small"
          @click="updateApplets"
        >一键替换</el-button>
      </el-col>

<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="small"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['applet:applet:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="small"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['applet:applet:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="small"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['applet:applet:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="appletList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="id" align="center" prop="id" /> -->
      <el-table-column label="appid" align="center" prop="appid" />
      <!-- <el-table-column label="secret" align="center" prop="secret" /> -->



      <!-- <el-table-column label="公众号logo" align="center" prop="gzhLogo" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.gzhLogo" :width="50" :height="50"/>
        </template>
      </el-table-column>

      <el-table-column label="二维码图片" align="center" prop="gzhQrCode" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.gzhQrCode" :width="50" :height="50"/>
        </template>
      </el-table-column> -->
    <el-table-column label="渠道信息" align="center" prop="gzhName" >
        <template slot-scope="scope">
           <span v-if="scope.row.channelCode == null" style="color: red">该小程序不可用（未创建渠道信息）</span>
          <span v-if="scope.row.channelCode != null"> {{ scope.row.channelName }} <br> 【{{ scope.row.channelCode }} 】</span>

          </template>
      </el-table-column>

      <el-table-column label="公众号名称｜二维码｜logo" align="center" prop="gzhName" >
        <template slot-scope="scope">
          {{ scope.row.gzhName }}  ｜
            <image-preview :src="scope.row.gzhQrCode" :width="50" :height="50"/>  ｜
            <image-preview :src="scope.row.gzhLogo" :width="50" :height="50"/>
          </template>
      </el-table-column>


      <!-- <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="公众号显示状态" align="center" prop="gzhStatus" >
        <template slot-scope="scope">
          <el-switch v-model="scope.row.gzhStatus" :active-value="1" :inactive-value="0"
                     @change="changeStatus(scope.row, $event)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['applet:applet:edit']"
          >修改</el-button>
          <el-button
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['applet:applet:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改小程序配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="appid" prop="appid">
          <el-input v-model="form.appid" placeholder="请输入appid" />
        </el-form-item>
        <el-form-item label="secret" prop="secret">
          <el-input v-model="form.secret" placeholder="请输入密钥secret" />
        </el-form-item>

        <el-form-item label="牌照类型" prop="licenseType">
          <el-select v-model="form.licenseType" clearable placeholder="请选择牌照类型" filterable>
            <el-option v-for="(item, i) in extData.statusTypeList || []" :key="i" :label="item.key"
                       :value="item.value"></el-option>
          </el-select>

        </el-form-item>
        <el-form-item label="应用类型" prop="status">
          <el-select v-model="form.status" clearable placeholder="请选择应用类型" filterable>
            <el-option v-for="(item, i) in extData.appletList || []" :key="i" :label="item.key"
                       :value="item.value"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="公众号名称" prop="gzhName">
          <el-input v-model="form.gzhName" placeholder="请输入公众号名称" />
        </el-form-item>

        <el-form-item label="公众号logo" prop="logoUrl">
          <ImageUpload v-model="form.gzhLogo" :limit="1" bizType="APPLETLOGO" type="cos" />
        </el-form-item>

        <el-form-item label="公众号二维码" prop="logoUrl">
          <ImageUpload v-model="form.gzhQrCode" :limit="1" bizType="APPLETLOGO" type="cos" />
        </el-form-item>

        <el-form-item label="校验文件" prop="filePath">
          <file-upload  v-model="form.filePath" baseUrlDiy="https://api.hlai.hengtdb.com/" :limit="1" />
        </el-form-item>



        <!-- <el-form-item label="渠道编码" prop="channelCode">
          <el-select v-model="form.channelCode" clearable placeholder="请选择渠道编码" filterable>
            <el-option v-for="(item, i) in extData.channelCodeList || []" :key="i" :label="item.value"
                       :value="item.value"></el-option>
          </el-select>

        </el-form-item> -->
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

<!--    一键修改-->
    <el-dialog :title="title" :visible.sync="open1" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules1" label-width="80px">
        <el-form-item label="牌照类型" prop="licenseType">
          <el-select v-model="form.licenseType" clearable placeholder="请选择牌照类型" filterable>
            <el-option v-for="(item, i) in extData.statusTypeList || []" :key="i" :label="item.key"
                       :value="item.value"></el-option>
          </el-select>

        </el-form-item>

        <el-form-item label="公众号名称" prop="gzhName">
          <el-input v-model="form.gzhName" placeholder="请输入公众号名称" />
        </el-form-item>

        <el-form-item label="公众号logo" prop="logoUrl">
          <ImageUpload v-model="form.gzhLogo" :limit="1" bizType="APPLETLOGO" type="cos" />
        </el-form-item>

        <el-form-item label="公众号二维码" prop="logoUrl">
          <ImageUpload v-model="form.gzhQrCode" :limit="1" bizType="APPLETLOGO" type="cos" />
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm1">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>



<script>
import {listApplet, getApplet, delApplet, addApplet, updateApplet, configInfoExtData,updateAppletInfo} from "@/api/applet/applet";

export default {
  name: "Applet",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 小程序配置表格数据
      appletList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open1: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        appid: null,
        secret: null,
        status: null,
        licenseType: null,
        createTime: null,
        updateTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules1:{
        licenseType: [
          { required: true, message: "牌照不能为空", trigger: "blur" }
        ],
        gzhName: [
          { required: true, message: "公众号名称不能为空", trigger: "blur" }
        ],
      },
      rules: {
        appid: [
          { required: true, message: "appid不能为空", trigger: "blur" }
        ],
        secret: [
          { required: true, message: "密钥secret不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "类型不能为空", trigger: "change" }
        ],
        // createTime: [
        //   { required: true, message: "创建时间不能为空", trigger: "blur" }
        // ],
        // updateTime: [
        //   { required: true, message: "修改时间不能为空", trigger: "blur" }
        // ],
        // gzhLogo: [
        //   { required: true, message: "小程序图片不能为空", trigger: "blur"}
        // ],
        // gzhQrCode: [
        //   {required: true, message: "二维码图片不能为空", trigger: "blur"}
        // ],
        // gzhName :[
        //   {required: true, message: "小程序名字不能为空", trigger: "blur"}
        // ],
        // channelCode: [
        //   {required: true, message: "渠道信息不能为空", trigger: "blur"}
        // ]
      },
      extData: {
        statusTypeList:[]
      },
    };
  },
  created() {
    this.getList();
    this.getOptions();
  },
  methods: {
    getOptions() {
      configInfoExtData().then((response) => {
        this.extData = response.data;
        console.log( this.extData)
      });
    },

    /** 查询小程序配置列表 */
    getList() {
      this.loading = true;
      // this.queryParams.licenseType ='HS';
      listApplet(this.queryParams).then(response => {
        this.appletList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        appid: null,
        secret: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        filePath: null,
        licenseType : null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加小程序配置";
    },
    /** 修改小程序状态*/
    changeStatus(row, value) {
      let text = (+value) === 0 ? "停用" : "启用";
      updateApplet({ id: row.id, gzhStatus: +value }).then(res => {
        this.$modal.msgSuccess(text + "成功");
        this.getList();
      }).catch(() => {
        row.gzhStatus = row.gzhStatus == "0" ? "1" : "0";
      })
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getApplet(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改小程序配置";
      });
    },
    /** 修改按钮操作 */
    updateApplets() {
      this.reset();
      this.open1 = true;
      this.title = "修改小程序配置";
    },
    submitForm1(){
      updateAppletInfo(this.form).then(response =>{
        this.$modal.msgSuccess("修改成功");
        this.open1 = false;
        this.getList();
      })
    },
    /** 提交按钮 */
    submitForm() {

      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateApplet(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addApplet(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除小程序配置编号为"' + ids + '"的数据项？').then(function() {
        return delApplet(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('applet/applet/export', {
        ...this.queryParams
      }, `applet_${new Date().getTime()}.xlsx`)
    },

    configInfoExtData(){

    }
  }
};
</script>
